package com.kotlin.myapplication.mvp.ui.main.fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.kotlin.myapplication.R
import com.kotlin.myapplication.base.BaseFragment
import com.kotlin.myapplication.bean.Weather
import com.kotlin.myapplication.bean.WeatherResult
import com.kotlin.myapplication.bean.realtime
import com.kotlin.myapplication.mvp.model.WeatherContract
import com.kotlin.myapplication.mvp.presenter.WeatherPresenter
import com.kotlin.myapplication.mvp.ui.main.adapter.SideslipListAdapter
import kotlinx.android.synthetic.main.fragment_my_devices.*
import com.kotlin.myapplication.mvp.ui.start.LoginActivity
import android.content.Intent
import android.text.TextUtils
import androidx.core.content.ContextCompat.startActivity
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.kotlin.myapplication.R.id.address
import com.kotlin.myapplication.R.id.temperature
import com.kotlin.myapplication.mananger.APPSPManager
import com.kotlin.myapplication.mvp.ui.main.fragment.adapter.TabViewPagerAdapter
import com.kotlin.myapplication.utils.Logger
import com.kotlin.myapplication.mvp.ui.main.fragment.fragment.TabFourFragment
import com.kotlin.myapplication.mvp.ui.main.fragment.fragment.TopFragment
import com.kotlin.myapplication.mvp.ui.main.fragment.fragment.TabThreeFragment
import com.kotlin.myapplication.mvp.ui.main.fragment.fragment.TabTwoFragment
import kotlinx.android.synthetic.main.fragment_my_devices.view.*
import kotlinx.android.synthetic.main.fragment_square.view.*


/**getWeather
 *@lil
 *@创建时间 2019/11/7 14:24
 *@描述
 */
class MyDevicesFragment : BaseFragment(), WeatherContract.view, View.OnClickListener {

    override lateinit var presenter: WeatherContract.Weather
    var list: ArrayList<Weather<WeatherResult<realtime>>>? = null
    lateinit var adapter: SideslipListAdapter
    //tab
    var tabs = ArrayList<String>()
    lateinit var mViewPagerAdapter: TabViewPagerAdapter
    var mFragmentList = ArrayList<Fragment>()

    override fun getView(inflater: LayoutInflater, parent: ViewGroup): View {
        return inflater.inflate(R.layout.fragment_my_devices, parent, false)
    }

    override fun initView(view: View) {
        presenter = WeatherPresenter(this)
        presenter.getWeather("南京")
//        initAdapter()
//        if (!!TextUtils.isEmpty(APPSPManager.getUserName())) {
//            main_view_top_layout.visibility = View.VISIBLE
//        } else {
        main_view_top_layout.visibility = View.GONE
//        }

        main_view_top_layout.setOnClickListener(this)
        initTabs()
    }

    //初始化tab
    fun initTabs() {
        tabs?.add("头条")
        tabs?.add("社会")
//        tabs?.add("国内")
//        tabs?.add("国际")
//        tabs?.add("娱乐")
//        tabs?.add("科技")
//        tabs?.add("体育")
//        tabs?.add("财经")
//        tabs?.add("时尚")
//        tabs?.add("军事")
        mViewPagerAdapter = TabViewPagerAdapter(fragmentManager!!, initFragment(), tabs)
        viewPager.adapter = mViewPagerAdapter
        styleChoice_tab.setViewPager(viewPager)

    }

    fun initFragment(): ArrayList<Fragment> {

        mFragmentList.add(TopFragment())
        mFragmentList.add(TabTwoFragment())
//        mFragmentList.add(TabThreeFragment())
//        mFragmentList.add(TabFourFragment())
        return mFragmentList
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.main_view_top_layout -> {
                login()
            }

        }
    }

    /**
     * 进入登录界面
     */
    private fun login() {
        val intent = Intent(activity, LoginActivity::class.java)
        startActivity(intent)
    }

    override fun showWeather(weather: Weather<WeatherResult<realtime>>) {
        address.setText(weather.result.city)
        temperature.setText(weather.result.realtime.temperature)
    }

    override fun error(error: String) {
        Logger.log(error)
    }
}